package com.pengliu.config;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.pengliu.service.HttpService;


@Component("taskConfig")
@EnableScheduling
@Configurable
public class TaskConfig {
	
	@Autowired
	private HttpService httpService;
	
	/**
	 * user: pengliu
	 * date: 2018年2月27日
	 * desc: 每日签到QQ阅读
	 * @throws Exception
	 * @author pengliu
	 * @throws IOException 
	 */
	@Scheduled(cron="0 0 2 * * ?")
    public void signQqReadBook() throws IOException {
		Date dateTime = new Date();
		
		Map<String,String> cookies = new HashMap<String,String>();
		cookies.put("ywkey", "ywV6X3ouqlfe");
		
		httpService.setCookies(cookies);
		String url = "https://newcommon.reader.qq.com/v6_6_8/checkin?weekday="+getWeekOfDate(dateTime)+"&isresign=0";
		Map<String,String> heads = new HashMap<String,String>();
		heads.put("Accept-Encoding","gzip");
		heads.put("User-Agent","okhttp/3.6.0");
		heads.put("c_platform","android");
		heads.put("c_version","qqreader_6.6.8.0888_android");
		heads.put("channel","00000");
		heads.put("ckey","001010000000");
		heads.put("gselect","1");
		heads.put("loginType","1");
		heads.put("mversion","6.6.8.888");
		heads.put("nosid","1");
		heads.put("os","meizu");
		heads.put("qimei","867905028086587");
		heads.put("qqnum","540891049");
		heads.put("qrem","0");
		heads.put("qrsy","0BA723E3DBF875A0C745AB409C0689EE");
		heads.put("qrtm",String.valueOf(System.currentTimeMillis()/1000));
		heads.put("safekey","B5B78FA158816E152225E449B6175370");
		heads.put("server_sex","1");
		heads.put("sid","1484040997493242");
		heads.put("supportmh","1");
		heads.put("supportTS","2");
		heads.put("themeid","1000");
		heads.put("tinkerid","qqreader_6.6.8.0888_android-common-357599");
		heads.put("ua","meizu_PRO5#PRO5#22");
		heads.put("ywguid","540891049");
		heads.put("ywkey","ywV6X3ouqlfe");
		
		String resultJson = "";
		
		try {
			resultJson = httpService.requestPost(url, null, heads);
		} catch (Exception e) {
			resultJson = e.getMessage();
		}
		
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日HH时mm分ss秒");
		String timeStr = sdf.format(dateTime);
		
		File file = new File("/logs/QQ阅读日志/"+timeStr+".txt");
		FileUtils.write(file, resultJson, "UTF-8", true);
		
	}
	
	public static String getWeekOfDate(Date dt) {
        String[] weekDays = {"7", "1", "2", "3", "4", "5", "6"};
        Calendar cal = Calendar.getInstance();
        cal.setTime(dt);
        int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
        if (w < 0)
            w = 0;
        return weekDays[w];
    }
}
